'''
pyDuinoAPI : Python Arduino API : Pinout Module
author:     Steve De Jongh ( dejongh[dot]st[at]gmail[dot]com )
Licence type: MIT

Copyright (c) 2013 Steve De Jongh (dejongh[dot]st[at]gmail[dot]com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'''

from pintypes import Pin, AnalogPin, DigitalPin, PwmPin
from constants import *

class Pinout(object):
    """
    General board Pinout Object. Used to configure pinout of the Arduino object
    """
    def __init__(self, pinout):
        self._pins = {}
        for pin in pinout:
            self._pins[pin[0]] = {
                "analog":AnalogPin(pin[0]),
                "digital":DigitalPin(pin[0]),
                "pwm":PwmPin(pin[0])
            }[pin[1]]

    def listPins(self,type=None):
        """
        Usage: Pinout.listPins(type)
        Returns a list of the pins of the given type.
        Type can be:
            None => returns all pins
            "analog" => returns all analog pins
            "digital" => returns all digital pins
            "pwm" => returns all pwm pins
        """
        if type == None:
            return sorted([pin for pin in self._pins.keys()])
        elif type == "analog":
            return sorted([pin for pin in self._pins.keys() if isinstance(self._pins[pin],AnalogPin)])
        elif type == "digital":
            return sorted([pin for pin in self._pins.keys() if isinstance(self._pins[pin],DigitalPin) and not isinstance(self._pins[pin],PwmPin)])
        elif type == "pwm":
            return sorted([pin for pin in self._pins.keys() if isinstance(self._pins[pin],PwmPin) ])
